package com.abc.task;

import com.abc.entity.Programme;
import com.abc.service.IProgrammeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ReminderTask {

    @Autowired
    private IProgrammeService programmeService;

    @Autowired
    private JavaMailSender mailSender;

    @Scheduled(fixedRate = 60000) // 每分钟执行一次
    public void checkReminders() {
        // 查询需要提醒的日程
        List<Programme> programmes = programmeService.getProgrammesToRemind();
        for (Programme programme : programmes) {
            // 发送提醒
            SimpleMailMessage message = new SimpleMailMessage();
            message.setTo(programme.getUserEmail());
            message.setSubject("日程提醒");
            message.setText("您有一条日程即将开始：" + programme.getContent());
            mailSender.send(message);

            // 更新提醒状态
            programme.setReminded(true);
            programmeService.updateById(programme);
        }
    }
}